import { Api } from './frontend-api-client.ts/AutoCostApiClient';

// 创建API客户端实例
const apiClient = new Api({
  baseURL: 'http://localhost:3000',
  headers: {
    'Content-Type': 'application/json',
  },
});

// 设置认证token
apiClient.setSecurityData({
  token: 'your-jwt-token-here',
});

// ===== 车辆管理示例 =====

// 创建燃油车
const createFuelCarExample = async () => {
  try {
    const fuelCar = await apiClient.api.carsCreate({
      name: '我的燃油车',
      category: '小汽车',
      brand: '丰田',
      carModel: '凯美瑞',
      productionTime: 1640995200000, // 2022-01-01
      purchaseTime: 1640995200000,
      vehicleType: 'fuel',
      licensePlate: '京A12345',
      currentMileage: 50000,
      fuelCapacity: 60,
      fuelType: '#92',
      purchasePrice: 250000,
      currency: 'CNY',
      notes: '家用轿车',
    });

    console.log('燃油车创建成功:', fuelCar.data);
    return fuelCar.data;
  } catch (error) {
    console.error('创建燃油车失败:', error);
  }
};

// 创建电动车
const createElectricCarExample = async () => {
  try {
    const electricCar = await apiClient.api.carsCreate({
      name: '我的电动车',
      category: '电车',
      brand: '特斯拉',
      carModel: 'Model 3',
      productionTime: 1640995200000,
      purchaseTime: 1640995200000,
      vehicleType: 'electric',
      licensePlate: '京B67890',
      currentMileage: 30000,
      batteryCapacity: 75,
      mileage: 500,
      purchasePrice: 350000,
      currency: 'CNY',
      notes: '纯电动轿车',
    });

    console.log('电动车创建成功:', electricCar.data);
    return electricCar.data;
  } catch (error) {
    console.error('创建电动车失败:', error);
  }
};

// 获取车辆列表
const getCarsExample = async () => {
  try {
    const cars = await apiClient.api.carsList({
      page: 1,
      limit: 10,
    });

    console.log('车辆列表:', cars.data);
    return cars.data;
  } catch (error) {
    console.error('获取车辆列表失败:', error);
  }
};

// 获取车辆详情
const getCarDetailExample = async (carId: string) => {
  try {
    const car = await apiClient.api.carsDetail(carId);

    console.log('车辆详情:', car.data);
    return car.data;
  } catch (error) {
    console.error('获取车辆详情失败:', error);
  }
};

// 更新车辆信息
const updateCarExample = async (carId: string) => {
  try {
    const updatedCar = await apiClient.api.carsUpdate(carId, {
      currentMileage: 55000,
      notes: '已更新里程数',
    });

    console.log('车辆更新成功:', updatedCar.data);
    return updatedCar.data;
  } catch (error) {
    console.error('更新车辆失败:', error);
  }
};

// 删除车辆
const deleteCarExample = async (carId: string) => {
  try {
    const result = await apiClient.api.carsDelete(carId);

    console.log('车辆删除成功:', result);
    return result;
  } catch (error) {
    console.error('删除车辆失败:', error);
  }
};

// ===== 默认车辆管理示例 =====

// 设置默认车辆
const setDefaultVehicleExample = async (vehicleId: string) => {
  try {
    const result = await apiClient.api.authSetDefaultVehicleUpdate({
      vehicleId: vehicleId,
    });

    console.log('默认车辆设置成功:', result);
    return result;
  } catch (error) {
    console.error('设置默认车辆失败:', error);
  }
};

// 获取默认车辆
const getDefaultVehicleExample = async () => {
  try {
    const result = await apiClient.api.authGetDefaultVehicleList();

    console.log('默认车辆信息:', result);
    return result;
  } catch (error) {
    console.error('获取默认车辆失败:', error);
  }
};

// ===== 用户认证示例 =====

// 用户注册
const registerExample = async () => {
  try {
    const result = await apiClient.api.authRegisterCreate({
      username: 'testuser',
      email: 'test@example.com',
      password: 'password123',
    });

    console.log('注册成功:', result);
    return result;
  } catch (error) {
    console.error('注册失败:', error);
  }
};

// 用户登录
const loginExample = async () => {
  try {
    const result = await apiClient.api.authLoginCreate({
      email: 'test@example.com',
      password: 'password123',
    });

    console.log('登录成功:', result);

    // 更新API客户端的认证token
    if (result.data?.token) {
      apiClient.setSecurityData({
        token: result.data.token,
      });
    }

    return result;
  } catch (error) {
    console.error('登录失败:', error);
  }
};

// ===== 费用管理示例 =====

// 创建费用记录
const createExpenseExample = async (vehicleId: string) => {
  try {
    const expense = await apiClient.api.expensesCreate({
      vehicleId: vehicleId,
      type: '加油',
      amount: 300,
      currency: 'CNY',
      date: '2024-01-15',
      description: '加油站加油',
      location: '中石化加油站',
      mileage: 50000,
    });

    console.log('费用记录创建成功:', expense.data);
    return expense.data;
  } catch (error) {
    console.error('创建费用记录失败:', error);
  }
};

// 获取费用列表
const getExpensesExample = async () => {
  try {
    const expenses = await apiClient.api.expensesList({
      page: 1,
      limit: 10,
    });

    console.log('费用列表:', expenses.data);
    return expenses.data;
  } catch (error) {
    console.error('获取费用列表失败:', error);
  }
};

// ===== 费用类型管理示例 =====

// 获取费用类型列表
const getExpenseTypesExample = async () => {
  try {
    const expenseTypes = await apiClient.api.configExpenseTypesList();

    console.log('费用类型列表:', expenseTypes.data);
    return expenseTypes.data;
  } catch (error) {
    console.error('获取费用类型列表失败:', error);
  }
};

// ===== 完整流程示例 =====

const completeWorkflowExample = async () => {
  console.log('=== 开始完整流程示例 ===');

  // 1. 用户登录
  const loginResult = await loginExample();
  if (!loginResult?.data?.token) {
    console.error('登录失败，无法继续');
    return;
  }

  // 2. 创建车辆
  const fuelCar = await createFuelCarExample();
  if (!fuelCar?._id) {
    console.error('创建车辆失败，无法继续');
    return;
  }

  // 3. 设置默认车辆
  await setDefaultVehicleExample(fuelCar._id);

  // 4. 获取默认车辆信息
  await getDefaultVehicleExample();

  // 5. 创建费用记录
  await createExpenseExample(fuelCar._id);

  // 6. 获取费用列表
  await getExpensesExample();

  console.log('=== 完整流程示例结束 ===');
};

// 导出示例函数
export {
  createFuelCarExample,
  createElectricCarExample,
  getCarsExample,
  getCarDetailExample,
  updateCarExample,
  deleteCarExample,
  setDefaultVehicleExample,
  getDefaultVehicleExample,
  registerExample,
  loginExample,
  createExpenseExample,
  getExpensesExample,
  getExpenseTypesExample,
  completeWorkflowExample,
};
